<template>
  <div class="newProject">
    <div style="width: 95%;height: 838px; margin: auto;margin-top: 20px;">
      <div style="width: 100%;height: 46px;align-items: center;border-bottom: 2px solid #DCDFE6;margin-bottom: 30px; display: flex;
  align-items: center;  ">
        <span
          style="font-family: PingFangSC-Medium;font-weight: 500;font-size: 16px;color: #303133;line-height: 24px;">编辑项目</span>
      </div>
      <div>
        <el-form ref="form" :model="projectInfo" :rules="rules">
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目编号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="no">
                <el-input v-model="projectInfo.no" placeholder="请输入项目编号" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>技术合同登记号：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-input style="height: 28px;" placeholder="请输入技术合同登记号" v-model="projectInfo.contractNo"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目名称：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-form-item label="" prop="name">
                <el-input v-model="projectInfo.name" placeholder="请输入项目名称" style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">立项单位：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="establishUnit">
                <el-input v-model="projectInfo.establishUnit" placeholder="请输入立项名称"
                  style="height: 28px !important;"></el-input>
                <!-- <el-select @change="changeSel" v-model="projectInfo.establishUnit" placeholder="请选择立项单位"
                  style="width: 100%; ">
                  <el-option v-for="item in unitOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select> -->
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目类型：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="type">
                <el-select @change="changeSel" v-model="projectInfo.type" placeholder="请选择项目类型" style="width: 100%; ">
                  <el-option v-for="item in projectOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目开始时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="establishTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="projectInfo.establishTime"
                  format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" style=" width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目完成时间：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="endTime">
                <el-date-picker type="date" placeholder="选择日期" v-model="projectInfo.endTime" format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd" style=" width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">项目总经费(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="totalFee">
                <el-input v-model="projectInfo.totalFee" placeholder="请输入项目总经费"
                  style="height: 28px !important;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span>项目到款额(万元)：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="">
                <el-input style="height: 28px;" placeholder="请输入项目到款额" v-model="projectInfo.receivedMoney"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 56px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span class="required">牵头部门：</span>
            </el-col>
            <el-col :span="6" class="colRight" prop="leadDeptId">
              <!-- <el-cascader :options="options" :show-all-levels="false" v-model="projectInfo.leadDeptId"
                placeholder="请选择牵头部门" :props="{ checkStrictly: true }" style="width: 100%;" clearable></el-cascader> -->
              <el-select @change="changeSel" v-model="projectInfo.leadDeptId" placeholder="请选择牵头部门" style="width: 100%; ">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="5" class="colLeft">
              <span class="required">项目下达性质：</span>
            </el-col>
            <el-col :span="6" class="colRight">
              <el-form-item label="" prop="nature">
                <el-select @change="changeSel" v-model="projectInfo.nature" placeholder="请选择任务下达性质" style="width: 100%; ">
                  <el-option v-for="item in natureOptions" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>项目描述：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入项目描述" v-model="projectInfo.description">
              </el-input>
            </el-col>
          </el-row>
          <el-row type="flex" justify="space-between"
            style="border: 1px solid #DCDFE6;height: 140px;background-color: #F5F7FA;">
            <el-col :span="5" class="colLeft">
              <span>备注：</span>
            </el-col>
            <el-col :span="18" class="colRight">
              <el-input type="textarea" :rows="5" placeholder="请输入备注" v-model="projectInfo.remark">
              </el-input>
            </el-col>
          </el-row>
          <el-form-item style="margin-top: 30px;">
            <el-button type="primary" @click="onSubmit" style="background-color: #47ACF4;">确定</el-button>
            <el-button type="primary" @click="closeDialog"
              style="margin-left: 30px;background: transparent; color: #47ACF4;margin-right:24px ;">取消</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { getDeptList, addProject, editProject } from "@/api/project/project";
export default {
  components: {
  },
  props: {
    projectInfo: {
      type: Object,
      required: false,
    }
  },
  data() {
    return {
      // form: {
      //   completionTime: '',
      //   contractNo: '',
      //   establishTime: '',
      //   establishUnit: '',
      //   delFlag: 0,
      //   id: '',
      //   leadDeptId: '',
      //   name: '',
      //   nature: '',
      //   no: '',
      //   receivedMoney: '',
      //   status: '',
      //   totalFee: '',
      //   type: '',
      //   description: '',
      //   remark: ''
      // },
      unitOptions: [],
      projectOptions: [],
      natureOptions: [],
      options: [],
      rules: {
        name: [
          { required: true, message: '请输入项目名称', trigger: 'blur' },
        ],
        establishTime: [
          { required: true, message: '请输入项目开始时间', trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '请输入项目完成时间', trigger: 'blur' },
        ],
        establishUnit: [
          { required: true, message: '请输入立项单位', trigger: 'blur' },
        ],
        leadDeptId: [
          { required: true, message: '请输入牵头部门', trigger: 'blur' },
        ],
        no: [
          { required: true, message: '请输入项目编号', trigger: 'blur' },
        ],
        totalFee: [
          { required: true, message: '请输入项目总经费', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请输入项目类型', trigger: 'blur' },
        ],
        nature: [
          { required: true, message: '请输入项目下达性质', trigger: 'blur' },
        ],
      }
    }
  },
  mounted() {
    getDeptList().then(res => {
      this.options = res.data[0].children
      console.log("dept", this.options);
    });
    this.natureOptions = this.getDict('xmgl_project_nature');
    this.unitOptions = this.getDict('xmgl_project_initiationunit');
    this.projectOptions = this.getDict('xmgl_project_type');
  },
  methods: {
    onSubmit() {
      // this.projectInfo.leadDeptId = this.projectInfo.leadDeptId[this.projectInfo.leadDeptId.length - 1]
      console.log('leadDeptId', this.projectInfo.leadDeptId)
      editProject(this.projectInfo).then(res => {
        if (res.code = 200) {
          alert("编辑项目成功！")
          this.closeDialog()
        }
      });
    },
    changeSel() {
      this.$forceUpdate()
    },
    closeDialog() {
      this.$emit('close-dialog'); // 触发自定义事件
    },
    getDict(dictName) {
      let dict = this.$store.state.dictNew[dictName]
      return dict
      // console.log('task', dict)
    }
  }
}
</script>

<style scoped>
.newProject {
  width: 95%;
  height: 100%;
  background-color: #FFFFFF;
  margin-top: 20px;

  .colRight {

    padding: 10px;
    background-color: #FFFFFF;
  }

  .colLeft {
    /* border: 1px solid #DCDFE6; */
    background-color: #F5F7FA;
    text-align: center;
    margin: auto;
  }
}

span.required::after {
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
</style>
<style>
.el-form-item__error {
  z-index: 999 !important;
}
</style>
